package Services

import(
	"github.com/gin-gonic/gin"
	"fmt"
	"net/http"
	"gin/Databases"
	"gin/Middlewares"
	"gin/Common"
	"strconv"
)

type EntityDataAddCgi struct {
	Common.CgiBase
}

func (cgi EntityDataAddCgi)quertEntity() (bool, int) {
	fmt.Println("quertEntity")
	var cnt []int
	sql := "select count(1) from db_entity_set_data where Fname=?;"
	err := Databases.Db.Select(&cnt, sql, cgi.Input["name"])
	if err != nil {
		fmt.Println("exec failed, ", err)
		return false,0
	}

    return true, cnt[0]
}

func (cgi EntityDataAddCgi)addEntity() (bool) {
	fmt.Println("addEntity:", cgi.Input["name"])
	class1, _ := strconv.Atoi(cgi.Input["class1"])
	class2, _ := strconv.Atoi(cgi.Input["class2"])

	sql := "insert into db_entity_set_data (Fname,Fdesc,Fclass1,Fclass2,Fpre_attr,Fpost_attr,Fentity_set,Fstatus,Fcreate_time,Fmodify_time) values (?,?,?,?,?,?,?,0,now(),now());"
	_,err := Databases.Db.Exec(sql, cgi.Input["name"], cgi.Input["desc"], class1, class2,
									cgi.Input["pre_attr"], cgi.Input["post_attr"], cgi.Input["entity_set"])
	if err != nil {
		return false
	}
	return true
}

func (cgi EntityDataAddCgi)updateEntity() (bool) {
	fmt.Println("updateEntity")
	class1, _ := strconv.Atoi(cgi.Input["class1"])
	class2, _ := strconv.Atoi(cgi.Input["class2"])

	sql := "update db_entity_set_data set Fdesc=?,Fclass1=?,Fclass2=?,Fpre_attr=?,Fpost_attr=?,Fentity_set=?,Fmodify_time=now() where Fname=?;"
	_,err := Databases.Db.Exec(sql, cgi.Input["desc"], class1, class2,
									cgi.Input["pre_attr"], cgi.Input["post_attr"], cgi.Input["entity_set"], cgi.Input["name"])
	if err != nil {
		return false
	}
	return true
}

func (cgi *EntityDataAddCgi)processParam(c *gin.Context) {
	fmt.Println("processParam")
	cgi.Input["name"] = c.PostForm("name")
	cgi.Input["desc"] = c.DefaultPostForm("desc", "")
	cgi.Input["pre_attr"] = c.DefaultPostForm("pre_attr", "")
	cgi.Input["post_attr"] = c.DefaultPostForm("post_attr", "")
	cgi.Input["entity_set"] = c.PostForm("entity_set")
	cgi.Input["class1"] = c.PostForm("class1")
	cgi.Input["class2"] = c.PostForm("class2")
}

// 实体集数据
func EntityDataAdd(c *gin.Context) {
	if (!Middlewares.CheckLogin(c)) {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "login fail",
	        "data": "{}",
	    })
	    return
	}

	var cgi EntityDataAddCgi
	cgi.Init(c)
	cgi.processParam(c)

	res, cnt := cgi.quertEntity()
	if (!res) {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "query fail",
	        "data": "{}",
	    })
	    return
	}
    if (0 == cnt) {
    	res = cgi.addEntity()
    } else {
    	res = cgi.updateEntity()
    }

    if (!res) {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "process fail",
	        "data": "{}",
	    })
		fmt.Println("process failed, ")
		return
	}

    c.JSON(http.StatusOK, gin.H{
        "result": 0,
        "msg":    "success",
        "data": "",
    })

}

